<?php
class StudentgroupsController extends AppController {

	var $name = 'Studentgroups';
	var $helpers = array('Html', 'Form');

	function index() {
		$this->Studentgroup->recursive = 0;
		$this->set('studentgroups', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Studentgroup', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('studentgroup', $this->Studentgroup->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Studentgroup->create();
			if ($this->Studentgroup->save($this->data)) {
				$this->Session->setFlash(__('The Studentgroup has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Studentgroup could not be saved. Please, try again.', true));
			}
		}
		$this->loadModel('Facultet');
		$facultets = $this->Facultet->find('list');
		//$courses = $this->Studentgroup->Course->find('list');
		$learningforms = $this->Studentgroup->Learningform->find('list');
		$learningtypes = $this->Studentgroup->Learningtype->find('list');
		$this->set(compact('facultets', /*'courses',*/ 'learningforms', 'learningtypes'));
		
	}
	
		function update_profession () {
		
	  if(!empty($this->data['Studentgroup']['facultet_id'])) {
	    $facultet_id = $this->data['Studentgroup']['facultet_id'];
	    $this->loadModel('Profession');
	    $options = $this->Profession->find('list', array('conditions' => array ('Profession.facultet_id' => $facultet_id)));
	    $this->set(compact('options'));
	  }	
	}
	
	function update_course () {
		
	  if(!empty($this->data['Studentgroup']['profession_id'])) {
	    $profession_id = $this->data['Studentgroup']['profession_id'];
	    $this->loadModel('Course');
	    $options = $this->Course->find('list', array('conditions' => array ('Course.profession_id' => $profession_id)));
	    $this->set(compact('options'));
	  }	
	}	
	

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Studentgroup', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Studentgroup->save($this->data)) {
				$this->Session->setFlash(__('The Studentgroup has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Studentgroup could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Studentgroup->read(null, $id);
		}
		$courses = $this->Studentgroup->Course->find('list');
		$learningforms = $this->Studentgroup->Learningform->find('list');
		$learningtypes = $this->Studentgroup->Learningtype->find('list');
		$this->set(compact('courses','learningforms','learningtypes'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Studentgroup', true));
			$this->redirect(array('action' => 'index'));
		}
		if ($this->Studentgroup->del($id)) {
			$this->Session->setFlash(__('Studentgroup deleted', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('The Studentgroup could not be deleted. Please, try again.', true));
		$this->redirect(array('action' => 'index'));
	}

}
?>